package Main;

import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 * Ca m'as gavé...
 * @author Emmanuel
 */
public class LireWav implements Runnable{
    
    private String path;
    
    public LireWav(String path) {
        this.path=path;
    }
    
    @Override
    public void run() {
        
        /* code from http://docs.oracle.com/javase/7/docs/technotes/guides/sound/programmer_guide/chapter7.html#114527 */
        
        int totalFramesRead = 0;
        File file = new File(path);
        
        try {
            AudioInputStream audioInputStream=AudioSystem.getAudioInputStream(file);
            int bytesPerFrame=audioInputStream.getFormat().getFrameSize();
            
            // Set an arbitrary buffer size of 2048 frames.
            int numBytes = 2048 * bytesPerFrame;
            byte[] audioBytes = new byte[numBytes];
            
            //try {
                int numBytesRead;
                int numFramesRead;
                // Try to read numBytes bytes from the file.
                while ((numBytesRead=audioInputStream.read(audioBytes)) != -1) {
                    
                    // Calculate the number of frames actually read.
                    numFramesRead = numBytesRead / bytesPerFrame;
                    totalFramesRead += numFramesRead;
                    // Here, do something useful with the audio data that's
                    // now in the audioBytes array...
                }
            //} catch (Exception ex) {
                // Handle the error...
            //}
        } catch (UnsupportedAudioFileException e) {
            throw new NotSupportedAudioFileException();
        } catch (IOException e) {
            throw new RuntimeIOException();
        }
    }
}
